import { createRouter, createWebHistory } from 'vue-router';
import { invoke } from "@tauri-apps/api/core";

// 定义路由
const routes = [
  {
    path: '/',
    name: '默认路径',
    redirect: '/login'
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/layout/404.vue')
  },
  {
    path: '/login',
    name: '登录',
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/main',
    name: '主窗口',
    component: () => import('@/layout/index.vue'),
    children: [
      {
        path: '/chat',
        name: '聊天',
        component: () => import('@/views/chat/index.vue')
      },
      {
        path: '/contact',
        name: '联系人',
        component: () => import('@/views/contact/index.vue')
      },
      {
        path: '/collect',
        name: '收藏',
        component: () => import('@/views/collect/index.vue')
      },
    ]
  },
  {
    path: '/setting',
    name: '设置',
    component: () => import('@/views/setting/index.vue')
  },
  // {
  //   path: '/voiceCall',
  //   name: '拨打语音',
  //   component: () => import('@/views/chat/calling/Voice.vue')
  // },
  {
    path: '/friendsCircle',
    name: '朋友圈',
    component: () => import('@/views/friendsCircle/index.vue')
  },
  {
    path: '/videoAccount',
    name: '视频号',
    component: () => import('@/views/videoAccount/index.vue')
  },
  {
    path: '/souyisou',
    name: '搜一搜',
    component: () => import('@/views/souyisou/index.vue')
  },
  {
    path: '/xcx',
    name: '小程序',
    component: () => import('@/views/xcx/index.vue')
  },
  {
    path: '/emoji',
    name: '表情包',
    component: () => import('@/views/chat/inputArea/emoji/index.vue')
  },

  {
    path: '/free',
    name: '自由窗口',
    component: () => import('@/views/freeWindow/index.vue')
  },

  {
    path: '/chat',
    name: '聊天模块',
    children: [
      {
        path: 'historyLogs',
        name: '历史聊天记录',
        component: () => import('@/views/chat/historyLogs/index.vue')
      },
      {
        path: 'addFriend',
        name: '添加朋友',
        component: () => import('@/views/chat/search/addFriend.vue')
      },
      {
        path: 'singleWindow',
        name: '独立聊天窗口',
        component: () => import('@/views/chat/singleWindow/chat.vue')
      },
    ]
  },

  {
    path: '/contact',
    name: '通讯录模块',
    children: [
      {
        path: 'manager',
        name: '通讯录管理',
        component: () => import('@/views/contact/manager/index.vue')
      },
      {
        path: 'setRemarkAndTag',
        name: '设置备注和标签',
        component: () => import('@/views/contact/friendPanel/setRemarkAndTag.vue')
      },
      {
        path: 'setPermissions',
        name: '设置朋友权限',
        component: () => import('@/views/contact/friendPanel/setPermissions.vue')
      },
    ]
  },
  {
    path: '/note',
    name: '笔记',
    component: () => import('@/views/note/index.vue')
  },
  {
    path: '/tray',
    name: '托盘',
    component: () => import('@/views/tray/index.vue')
  },
  {
    path: '/demo',
    name: '演示',
    component: () => import('@/views/demo/index.vue')
  },
]

// 创建路由及配置
const router = createRouter({
  mode: 'hash',
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 设置全局前置守卫
// 白名单路由
const whiteList = ['/tray', '/free']
router.beforeEach(async (to, from, next) => {

  console.log('路由to', to)
  if(whiteList.indexOf(to.path) !== -1) {
    next()
  } else {
    // 检查路由是否存在
    const routeExists = router.options.routes.some(route =>
      route.path === to.path || route.path === to.matched[0]?.path
    );

    if (!routeExists && to.path !== '/404') {
      // 如果路由不存在且不是已经在 404 页面，则重定向到 404
      next('/404');
      return;
    }

    // 此处登录状态保存在后端中，否则跨窗口会有问题
    const loginStatus = await invoke('get_login_status');
    console.log('登录状态', loginStatus)

    // 如果已登录
    if (loginStatus) {
      if (to.path === '/login') {
        // 如果已登录且目标路径是 /login，则重定向到 /chat
        next('/chat');
      } else {
        next(); // 继续导航
      }
    } else {
      if (to.path !== '/login') {
        next('/login'); // 如果未登录且目标路径不是 /login，则重定向到 /login
      } else {
        next(); // 继续导航
      }
    }
  }
})

export default router
